package com.houxy.controller;

import com.houxy.repository.model.entity.Notice;
import com.houxy.repository.model.vo.NoticeVO;
import com.houxy.service.NoticeService;
import com.houxy.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Hou
 * @date 2019/3/19 10:30
 * @describe：
 */
@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView modelAndView = new ModelAndView();
        List<Notice> noticeList = noticeService.findAll();
        List<NoticeVO> noticeVOList = new ArrayList<>();
        if (noticeList != null && noticeList.size() > 0) {
            noticeList.stream().forEach(notice -> {
                String createTime = DateUtil.DateToStringYYYYMMDDHHMMSS(notice.getCreateTime());
                //String updateTime = DateUtil.DateToStringYYYYMMDDHHMMSS(notice.getUpdateTime());
                NoticeVO noticeVO = new NoticeVO(notice.getId(), notice.getTitle(), notice.getContent(), createTime);
                noticeVOList.add(noticeVO);
            });
        }
        modelAndView.addObject("noticeList", noticeVOList);
        modelAndView.setViewName("front/notice/noticeList");
        return modelAndView;
    }

    @RequestMapping("/findById")
    public ModelAndView findById(int id) {
        ModelAndView modelAndView = new ModelAndView();
        Notice notice = noticeService.findById(id);
        String createTime = DateUtil.DateToStringYYYYMMDDHHMMSS(notice.getCreateTime());
        NoticeVO noticeVO = new NoticeVO(notice.getId(), notice.getTitle(), notice.getContent(), createTime);
        modelAndView.addObject("notice", noticeVO);
        modelAndView.setViewName("front/notice/noticeDetail");
        return modelAndView;
    }
}
